﻿using System;
using System.Collections.Generic;
using System.IO;
using Ionic.Zip;
using NLog;

namespace Labthink.PCSoft.UniversalHost4.Utility
{
    public class ZipTool
    {
        //private static readonly RunningLog Logger = RunningLog.CreateLogger("Utility");//运行日志
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="sourceFile"></param>
        /// <param name="targetFile"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static int Zip(string sourceFile, string targetFile, string pwd)
        {
            try
            {
                if (File.Exists(targetFile)) File.Delete(targetFile);
                using (var zip = new ZipFile(targetFile))
                {
                    if (!string.IsNullOrEmpty(pwd)) zip.Password = pwd;
                    zip.AddFile(sourceFile, "");
                    zip.Save();
                }
                return 1;

            }
            catch (Exception e)
            {
                Logger.Error("压缩{0}失败...", e, sourceFile);
                return -1;
            }
        }
        /// <summary>
        /// 压缩文件夹
        /// </summary>
        /// <param name="sourceDir"></param>
        /// <param name="targetFile"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static int ZipDir(string sourceDir, string targetFile, string pwd)
        {
            try
            {
                if (File.Exists(targetFile)) File.Delete(targetFile);
                using (var zip = new ZipFile(targetFile))
                {
                    if (!string.IsNullOrEmpty(pwd)) zip.Password = pwd;
                    zip.AddDirectory(sourceDir);
                    zip.Save();
                }

                return 1;
            }
            catch (Exception e)
            {
                Logger.Error("压缩{0}失败...", e, sourceDir);
                return -1;
            }
        }

        public static int ZipDir(string sourceDir, string targetFile, string pwd, int max)
        {
            try
            {
                if (File.Exists(targetFile)) File.Delete(targetFile);
                using (var zip = new ZipFile(targetFile))
                {
                    zip.MaxOutputSegmentSize = max * 1024 * 1024;
                    zip.BufferSize = 1024;
                    zip.CaseSensitiveRetrieval = true;
                    if (!string.IsNullOrEmpty(pwd)) zip.Password = pwd;
                    zip.AddDirectory(sourceDir);
                    zip.Save();
                }

                return 1;
            }
            catch (Exception e)
            {
                Logger.Error("压缩{0}失败...", e, sourceDir);
                return -1;
            }
        }
        /// <summary>
        /// 压缩多个文件
        /// </summary>
        /// <param name="sourceFiles"></param>
        /// <param name="targetFile"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static int Zip(List<string> sourceFiles, string targetFile, string pwd)
        {
            try
            {
                if (File.Exists(targetFile)) File.Delete(targetFile);
                using (var zip = new ZipFile(targetFile))
                {
                    if (!string.IsNullOrEmpty(pwd)) zip.Password = pwd;
                    foreach (string f in sourceFiles)
                    {
                        zip.AddFile(f, "");
                    }
                    zip.Save();
                }
                return sourceFiles.Count;

            }
            catch (Exception e)
            {
                Logger.Error("压缩{0}个文件失败...", e, sourceFiles.Count);
                return -1;
            }
        }

        /// <summary>
        /// 解压文件
        /// </summary>
        /// <param name="sourceFile"></param>
        /// <param name="targetDir"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static string UnZip(string sourceFile, string targetDir, string pwd)
        {
            if (Directory.Exists(targetDir)) Directory.Delete(targetDir, true);
            try
            {
                using (ZipFile zip = ZipFile.Read(sourceFile))
                {
                    if (!string.IsNullOrEmpty(pwd)) zip.Password = pwd;
                    foreach (ZipEntry entry in zip)
                    {
                        entry.Extract(targetDir);

                    }

                    return targetDir;
                }
            }
            catch (Exception e)
            {
                Logger.Error("解压文件{0}失败...", e, sourceFile);
                return string.Empty;
            }
        }

        public static string UnZipAll(string sourceFile, string targetDir, string pwd)
        {
            if (Directory.Exists(targetDir)) Directory.Delete(targetDir, true);
            try
            {
                using (ZipFile zip = ZipFile.Read(sourceFile))
                {
                    if (!string.IsNullOrEmpty(pwd)) zip.Password = pwd;

                    zip.ExtractAll(targetDir);
                    return targetDir;
                }
            }
            catch (Exception e)
            {
                Logger.Error("解压文件{0}失败...", e, sourceFile);
                return string.Empty;
            }
        }
    }
}
